<#include "license.ftl">
<@license/>
<#assign object = doc.object>
package ${object.@package}.gwt.model;

<#list doc["/object/attributes/enum[@scope='global']"] as enum>
import ${object.@package}.gwt.model.enums.${enum.@class};
</#list>
import java.util.Date;
import java.util.HashMap;

import redora.client.EnumDisplay;
import redora.client.Persistable;

import ${object.@package}.gwt.constants.Constants;
import ${object.@package}.gwt.model.fields.${object.@name}Fields;

import com.google.gwt.view.client.ProvidesKey;

/**
<#if object.description[0]??>
* ${object.description}
</#if>
 * @author Redora (www.redora.net)
*/
public class ${object.@name} implements Persistable {

    public Long id;
    @Override
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Date creationDate;
    public Date getCreationDate() {
        assertList();
        return this.creationDate;
    }
    public Date updateDate;
    public Date getUpdateDate() {
        assertList();
        return this.updateDate;
    }
<#list doc["/object/attributes/enum[@scope='local']"] as att>
    <#if att.description[0]??>
    /** ${att.description} */
    </#if>
    public enum ${att.@class} implements EnumDisplay {
        <#list att.element as value>
            <#if value.@description[0]??>
        /** ${value.@description} */
            </#if>
        ${value.@name}(
            ((Constants)com.google.gwt.core.client.GWT.create(Constants.class)).${object.@name?uncap_first}_${att.@fieldName}_${value.@name}_table()
            , ((Constants)com.google.gwt.core.client.GWT.create(Constants.class)).${object.@name?uncap_first}_${att.@fieldName}_${value.@name}_form())<#if value_has_next>,<#else>;</#if>
        </#list>

        String displayForm;
        String displayTable;

        ${att.@class}(String table, String form) {
            displayTable = table;
            displayForm = form;
        }

        @Override
        public String displayForm() {
            return displayForm;
        }

        @Override
        public String displayTable() {
            return displayTable;
        }

        /** @return Null, or the result of valueOf() */
        public static ${att.@class} valueOfNullSafe(String value) {
            if (value != null && value.length() > 0)
                return valueOf(value);
            return null; 
        }
    }
</#list>

<#list object.formScope?children as att>
    <#if att?node_type == "element">
        <#if att?node_name == "date" || att?node_name == "datetime">
    public Date ${att.@fieldName};
        <#else>
    public ${att.@className} ${att.@fieldName};
        </#if>

    </#if>
</#list>

    public final HashMap<redora.client.util.Field, Object> dirty = new HashMap<redora.client.util.Field, Object>();
    public boolean isNew = true;
    public redora.client.Persistable.Scope fetchScope;
    @Override
    public redora.client.Persistable.Scope scope() {
        return fetchScope;
    }

    /**
    * The generated finder methods are added to this enum. Also the GWT client has
    * the same DefaultFinder, ordered in exactly the same manner. Therefor it is
    * possible to use the enum's ordinal position:
    * <code>
    * int ord = GwtClientObject.DefaultFinder.FindAll.ordinal();
    * if (ClientObjectSQL.DefaultFinder.values()[ord].name().equals("FindAll") {
    *    System.out.print("Hello World");
    * }
    * </code>
    */
    public enum DefaultFinder {
        FindAll
<#list doc["//attributes/*/finder"] as finder> //TODO add json filter
        , ${finder.@name?cap_first}
</#list>
<#list doc["//queries/*/finder"] as finder>
    <#if finder?parent.@javadoc[0]??>
        /** ${finder?parent.@javadoc} */
    </#if>
        , ${finder.@name?cap_first}
</#list>
    }

@Deprecated
    public ${object.@name}() {
<#list doc["/object/attributes/*[@default]"] as att>
    <#if att?node_name == "boolean">
        set${att.@fieldName?cap_first}(Boolean.${att.@default?upper_case});
    <#elseif att?node_name == "string">
        set${att.@fieldName?cap_first}("${att.@default}");
    <#elseif att?node_name == "integer" || att?node_name == "long" || att?node_name == "double">
        set${att.@fieldName?cap_first}(${att.@className}.valueOf(${att.@default}));
    <#elseif att?node_name == "enum">
        set${att.@fieldName?cap_first}(${att.@className}.${att.@default});
    <#else>
        error - undefined default ${att?node_name}
    </#if>
</#list>
        fetchScope = redora.client.Persistable.Scope.Form;
        isNew = true;
    }

    /**
    * Initializes a new (isNew = true) ${object.@name} and default values are set for:<br>
<#list doc["/object/attributes/*[@default]"] as att>
    * ${att.@fieldName} - ${att.@default};<br>
</#list>
    */
    public ${object.@name}(redora.client.Persistable.Scope scope) {
        this.fetchScope = scope;
<#list doc["/object/attributes/*[@default]"] as att>
    <#if att?node_name == "boolean">
        set${att.@fieldName?cap_first}(Boolean.${att.@default?upper_case});
    <#elseif att?node_name == "string">
        set${att.@fieldName?cap_first}("${att.@default}");
    <#elseif att?node_name == "integer" || att?node_name == "long" || att?node_name == "double">
        set${att.@fieldName?cap_first}(${att.@className}.valueOf(${att.@default}));
    <#elseif att?node_name == "enum">
        set${att.@fieldName?cap_first}(${att.@className}.${att.@default});
    <#else>
        error - undefined default ${att?node_name}
    </#if>
</#list>
        isNew = true;
    }

<#list object.formScope?children as att>
    <#if att.description[0]??>
    /** ${att.description} */
    </#if>
    <#if att?node_type == "element" && att?node_name != "object">
    public void set${att.@fieldName?cap_first}(<#if att?node_name == "date" || att?node_name == "datetime">Date<#else>${att.@className}</#if> ${att.@fieldName}){
       if ((${att.@fieldName} == null ? this.${att.@fieldName} != null : !${att.@fieldName}.equals(this.${att.@fieldName}))
                && !dirty.containsKey(${object.@name}Fields.${att.@fieldName})) {
            dirty.put(${object.@name}Fields.${att.@fieldName}, this.${att.@fieldName});
        }
        this.${att.@fieldName} = ${att.@fieldName};
    }

        <#if att.description[0]??>
    /** ${att.description} **/
        </#if>
    public ${att.@className} get${att.@fieldName?cap_first}() {
        <#if att.@list != "true">
        assertList();
        </#if>
        return ${att.@fieldName};
    }
    </#if>
</#list>

    /**
    * {@inheritDoc}
    */
    public HashMap<redora.client.util.Field, Object> dirty() {
        return dirty;
    }

    @Override
    /** Checks primarily on Id, if there is no Id, it will try hashCode. */
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (!(that instanceof ${object.@name})) {
            return false;
        }
        ${object.@name} t = (${object.@name})that;
        if (id != null && id.equals(t.id)) {
            return true;
        }
        return id == null && t.id == null && hashCode() == t.hashCode();
    }

    @Override
    public String toListString() {
        StringBuilder s = new StringBuilder();
        <#if object.listScope[0]??>
            <#list object.listScope?children as att>
                <#if att?node_name != "object">
        s.append(this.get${att.@fieldName?cap_first}()).append(" ");
                </#if>
            </#list>
        </#if>
        return s.toString().trim();
    }

    /**
     * The key provider that provides the unique ID of a ${object.@name}.
     */
    public static final ProvidesKey<${object.@name}> KEY_PROVIDER = new ProvidesKey<${object.@name}>() {
        public Object getKey(${object.@name} item) {
            return item == null ? null : item.getId();
        }
    };

    void assertList() {
        assert fetchScope != Persistable.Scope.List : "Not available when fetched as Scope.List. Fetch the object with Table or Form scope instead.";
    }
}
